import 'swiper/scss'
import 'swiper/scss/navigation'
import 'swiper/scss/pagination'
import 'element-plus/dist/index.css'
import { createApp } from 'vue'
import router from './router'
import storage from './storage'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueLazyload from 'vue-lazyload'
import VueCookies from 'vue-cookies'
import App from './App.vue'
import { createPinia } from 'pinia'
import { ElMessage, ElInfiniteScroll } from 'element-plus'
import 'element-plus/es/components/message/index'

const mock = true
if (mock) {
  import('./mock/user')
  import('./mock/carts')
  import('./mock/orders')
  import('./mock/pay')
  import('./mock/shippings')
  import('./mock/products')
}
// 根据前端的跨域方式做调整，使用代理方式
axios.defaults.baseURL = '/api'
axios.defaults.timeout = 8000
// 响应拦截器，接口错误拦截
axios.interceptors.response.use(response => {
  let res = response.data
  let path = window.history.state.current
  if (res.status === 0) {
    return Promise.resolve(res.data)
  } else if (res.status === 10) {
    if (path !== '/#/index') {
      window.location.href = '/#/login'
    }
    return Promise.reject(res.data)
  } else {
    ElMessage({
      message: '服务器有错误，请重新操作',
      type: 'warning',
    })
    return Promise.reject(res.msg)
  }
}, (error) => {
  let res = error.response
  ElMessage.error(res.data.message)
  return Promise.reject(error)
})
const app = createApp(App)
const pinia = createPinia()
app.config.globalProperties.$storage = storage
app.config.globalProperties.$message = ElMessage
app.use(VueAxios, axios).use(ElInfiniteScroll).use(VueCookies).use(VueLazyload, { loading: '/mimall/imgs/loading-svg/loading-bars.svg' })
  .use(pinia).use(router).mount('#app')
